package com.le.tester.leetcode;

import java.util.*;

/**
 * createTime：2023/1/3 14:31
 * description：数组后出现次数最多的数字
 */
public class MostFrequent {
    public int mostFrequent(int[] nums, int key) {

        Map<Integer, Integer> resultMap = new HashMap<>();
        int result = 0;
        int length = nums.length - 1;
        for (int i = 0; i <= length - 1; i++) {
            if (nums[i] == key) {
                if (Objects.isNull(resultMap.get(nums[i + 1]))) {
                    resultMap.put(nums[i + 1], 1);
                } else {
                    Integer oldTimeNum = resultMap.get(nums[i + 1]);
                    resultMap.put(nums[i + 1], oldTimeNum + 1);
                }
            }
        }

        Integer max = Collections.max(resultMap.values());

        for (Integer keyItem : resultMap.keySet()) {
            if (resultMap.get(keyItem).equals(max)) {
                return keyItem;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[] ints = {1, 100, 200, 1, 100};

        System.out.println(new MostFrequent().mostFrequent(ints, 1));
    }
}
